home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / dynarr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-08  |  5.6 KB  |  179 lines

  1. /* Simple 'n' stupid dynamic-array module.
  2.    Copyright (C) 1993 Sun Microsystems, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with:  Not in FSF. */
  21.  
  22. /* Written by Ben Wing, December 1993. */
  23.  
  24. /*
  25.  
  26. A "dynamic array" is a contiguous array of fixed-size elements where there
  27. is no upper limit (except available memory) on the number of elements in the
  28. array.  Because the elements are maintained contiguously, space is used
  29. efficiently (no per-element pointers necessary) and random access to a
  30. particular element is in constant time.  At any one point, the block of memory
  31. that holds the array has an upper limit; if this limit is exceeded, the
  32. memory is realloc()ed into a new array that is twice as big.  Assuming that
  33. the time to grow the array is on the order of the new size of the array
  34. block, this scheme has a provably constant amortized time (i.e. average
  35. time over all additions).
  36.  
  37. When you add elements or retrieve elements, pointers are used.  Note that
  38. the element itself (of whatever size it is), and not the pointer to it,
  39. is stored in the array; thus you do not have to allocate any heap memory
  40. on your own.  Also, returned pointers are only guaranteed to be valid
  41. until the next operation that changes the length of the array.
  42.  
  43. This is a container object.  Declare a dynamic array of a specific type
  44. as follows:
  45.  
  46. struct mytype_dynarr
  47. {
  48.   Dynarr_declare (mytype);
  49. };
  50.  
  51. Use the following functions/macros:
  52.  
  53.    void *Dynarr_new(type)
  54.       [MACRO] Create a new dynamic-array object, with each element of the
  55.       specified type.  The return value is a void * and must be cast to the
  56.       proper dynamic array type.
  57.    Dynarr_add(d, el)
  58.       [MACRO] Add an element to the end of a dynamic array.  EL is a pointer
  59.       to the element; the element itself is stored in the array, however.
  60.       No function call is performed unless the array needs to be resized.
  61.    Dynarr_add_many(d, base, len)
  62.       [MACRO] Add LEN elements to the end of the dynamic array.  The elements
  63.       should be contiguous in memory, starting at BASE.
  64.    Dynarr_insert_many_at_start(d, base, len)
  65.       [MACRO] Append LEN elements to the beginning of the dynamic array.
  66.       The elements should be contiguous in memory, starting at BASE.
  67.    Dynarr_insert_many(d, base, len, start)
  68.       Insert LEN elements to the dynamic arrary starting at position
  69.       START.  The elements should be contiguous in memory, starting at BASE.
  70.    int Dynarr_length(d)
  71.       [MACRO] Return the number of elements currently in a dynamic array.
  72.    type Dynarr_at(d, i)
  73.       [MACRO] Return the element at the specified index (no bounds checking
  74.       done on the index).  The element itself is returned, not a pointer
  75.       to it.
  76.    type *Dynarr_atp(d, i)
  77.       [MACRO] Return a pointer to the element at the specified index (no
  78.       bounds checking done on the index).  The pointer may not be valid
  79.       after an element is added to or removed from the array.
  80.    Dynarr_reset(d)
  81.       [MACRO] Reset the length of a dynamic array to 0.
  82.    Dynarr_free(d)
  83.       Destroy a dynamic array and the memory allocated to it.
  84.  
  85. Use the following global variable:
  86.  
  87.    Dynarr_min_size
  88.       Minimum allowable size for a dynamic array when it is resized.  The
  89.       default is 32 and does not normally need to be changed.
  90.  
  91. */
  92.  
  93. #include <config.h>
  94. #include "lisp.h"
  95.  
  96. int Dynarr_min_size = 1;
  97.  
  98. void *
  99. Dynarr_newf (int elsize)
  100. {
  101.   Dynarr *d = (Dynarr *) xmalloc (sizeof (Dynarr));
  102.  
  103.   memset (d, 0, sizeof (*d));
  104.   d->elsize = elsize;
  105.  
  106.   return d;
  107. }
  108.  
  109. void
  110. Dynarr_resize (void *d, int size)
  111. {
  112.   int newsize;
  113.   double multiplier;
  114.   Dynarr *dy = (Dynarr *) d;
  115.  
  116.   if (dy->max <= 8)
  117.     multiplier = 2;
  118.   else
  119.     multiplier = 1.5;
  120.  
  121.   for (newsize = dy->max; newsize < size;)
  122.     newsize = max (Dynarr_min_size, multiplier * newsize);
  123.  
  124.   /* Don't do anything if the array is already big enough. */
  125.   if (newsize > dy->max)
  126.     {
  127.       dy->base = xrealloc (dy->base, newsize*dy->elsize);
  128.       dy->max = newsize;
  129.     }
  130. }
  131.  
  132. /* Add a number of contiguous elements to the array starting at START. */
  133. void
  134. Dynarr_insert_many (void *d, void *el, int len, int start)
  135. {
  136.   Dynarr *dy = (Dynarr *) d;
  137.  
  138.   Dynarr_resize (dy, dy->cur+len);
  139.   /* Silently adjust start to be valid. */
  140.   if (start > dy->cur)
  141.     start = dy->cur;
  142.   else if (start < 0)
  143.     start = 0;
  144.  
  145.   if (start != dy->cur)
  146.     {
  147.       memmove ((char *) dy->base + (start + len)*dy->elsize,
  148.            (char *) dy->base + start*dy->elsize,
  149.            (dy->cur - start)*dy->elsize);
  150.     }
  151.   memcpy ((char *) dy->base + start*dy->elsize, el, len*dy->elsize);
  152.   dy->cur += len;
  153.  
  154.   if (dy->cur > dy->largest)
  155.     dy->largest = dy->cur;
  156. }
  157.  
  158. void
  159. Dynarr_delete_many (void *d, int start, int len)
  160. {
  161.   Dynarr *dy = (Dynarr *) d;
  162.  
  163.   assert (start >= 0 && len >= 0 && start + len <= dy->cur);
  164.   memmove ((char *) dy->base + start*dy->elsize,
  165.        (char *) dy->base + (start + len)*dy->elsize,
  166.        (dy->cur - start - len)*dy->elsize);
  167.   dy->cur -= len;
  168. }
  169.  
  170. void
  171. Dynarr_free (void *d)
  172. {
  173.   Dynarr *dy = (Dynarr *) d;
  174.  
  175.   if (dy->base)
  176.     xfree (dy->base);
  177.   xfree (dy);
  178. }
  179.